var pie_sys_quality = echarts.init(document.getElementById('pie_sys_quality'), 'shine');
var pie_sys_finish = echarts.init(document.getElementById('pie_sys_finish'), 'shine');
var bar_mass = echarts.init(document.getElementById('bar_mass'), 'shine');
var line_jiaoliao = echarts.init(document.getElementById('line_jiaoliao'), 'shine');
var bar_energy = echarts.init(document.getElementById('bar_energy'), 'shine');
var isCarousel = true;
var dindex = 0;
var count = 0;

function DataPro() {
	$("#time").html(utils.GetDateTime("yyyy-MM-dd HH:mm:ss"));
	if (count == 0 || count % 60 == 0) {
		ProdRefresh();
		QualityRefresh();
		EnergyRefresh();
	}
	count++;
}
window.onload = function() {
	var timer = setInterval(function() {
		DataPro();
	}, 1000);
}

function EnergyRefresh() {
	api.GetIndexEnergyInfo(function(result) {
		if (result.Code == 0) {
			var data = JSON.parse(result.Data);
			var rate = data.rate;
			// if(rate.length>0){
			// 	$("#electNum").html(rate[0].TotalPower);
			// 	$("#waterNum").html(rate[0].TotalKQ.substring(0,5));
			// 	$("#steamNum").html((Math.round(rate[0].TotalZQ/10)/100).toString().substring(0,5));
			// }
			var power = data.power;
			var xpower = new Array();
			var ypower = new Array();
			for (var i = 0; i < power.length; i++) {
				xpower.push(power[i].dateTime);
				ypower.push(Math.round(power[i].Value/100)/100);
			}
			var air = data.air;
			var xair = new Array();
			var yair = new Array();
			for (var i = 0; i < air.length; i++) {
				xair.push(air[i].dateTime);
				yair.push(Math.round(air[i].Value/100)/100);
			}
			var steam = data.steam;
			var xsteam = new Array();
			var ysteam = new Array();
			for (var i = 0; i < steam.length; i++) {
				xsteam.push(steam[i].dateTime);
				ysteam.push(steam[i].Value);
			}
			bar_energy.clear();
			bar_energy.setOption(option.getEnergyBar({
				title: {
					text: ' '
				},
				legend: {
					data: ['电力', '空气', '蒸汽']
				},
				xAxis: [{
					data: xpower
				}, {
					data: xair
				}, {
					data: xsteam
				}],
				series: [{
					name: '电力',
					type: 'pictorialBar',
					data: ypower
				}, {
					name: '空气',
					type: 'bar',
					data: yair
				}, {
					name: '蒸汽',
					type: 'bar',
					data: ysteam
				}]
			}));
			$("#electNum").html(utils.CalcCurNum(ypower[6]*10000),2);
			$("#waterNum").html(utils.CalcCurNum(yair[6]*10000),2);
			$("#steamNum").html(utils.CalcCurNum(ysteam[6]),2);
		}
	})
}
function GetEnergyInfo(args) {
	api.GetEnergyInfo(args, function(result) {
		var data = new Array();
		if (result.Code == 0) {
			var tmp = JSON.parse(result.Data);
			if (tmp.Rows.result == "1" && tmp.table1.length > 0) {
				for (var i = 0; i < tmp.table1.length; i++) {
					data.push(tmp.table1[i].Value)
				}
			}
		}
		return data;
	})
}
function ProdRefresh() {
	api.GetYiedBoardInfo(function(result) {
		if (result.Code == 0) {
			var data = JSON.parse(result.Data);
			var CurYied = data.CurYied;
			var PlanYied = data.PlanYied;
			$("#produce_Num").html(CurYied.Curring);
			$("#store_Num").html(CurYied.Storage);
			pie_sys_finish.clear();
			pie_sys_finish.setOption(option.getProdGauge({
				series: [{
					name: "入库完成率",
					data: [{
						value: Math.round(CurYied.Storage * 10000 / PlanYied.Storage) / 100,
						name: "入库完成率"
					}]
				}, {
					name: "生产完成率",
					data: [{
						value: Math.round(CurYied.Curring * 10000 / PlanYied.Curring) / 100,
						name: "生产完成率"
					}]
				}]
			}));
			var Series = data.Series;
			bar_mass.clear();
			bar_mass.setOption(option.getYiedBar({
				legend: {
					data: ['产量']
				},
				xAxis: [{
					data: Series.xAxis
				}],
				series: [{
					name: '产量',
					data: Series.yAxis
				}]
			}));
		}
	})
}

function QualityRefresh() {
	api.GetQualityInfo(function(result) {
		if (result.Code == 0) {
			var data = JSON.parse(result.Data);
			pie_sys_quality.clear();
			pie_sys_quality.setOption(option.getQuiltyGauge({
				yAxis: {
					data: ['综合', '外观', 'X 光']
				},
				series: {
					data: [data.Today.A_Rate, data.Today.W_Rate, data.Today.X_Rate]
				}
			}));
			line_jiaoliao.clear();
			line_jiaoliao.setOption(option.getQuiltyLine({
				legend: {
					data: ['合格率']
				},
				xAxis: {
					data: data.Series.xAxis
				},
				yAxis: [{
					max: 101,
					min: 97
				}],
				series: [{
					name: '合格率',
					data: data.Series.yAxis
				}]
			}));
		}
	});
}
window.onresize = function() {
	pie_sys_quality.resize();
	pie_sys_finish.resize();
	bar_mass.resize();
	line_jiaoliao.resize();
	bar_energy.resize();
}